﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace Could.Service
{
    class BaseData
    {
        static public string GetConnectionString()
        {
            //string connectionString = ConfigurationManager.ConnectionStrings["sphinx_cat"].ConnectionString;
            System.Configuration.Configuration webconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/WebSite1");
            System.Configuration.ConnectionStringSettings connsets ;
            string connectionString = "";
            if (webconfig.ConnectionStrings.ConnectionStrings.Count>0)
            {
                connsets = webconfig.ConnectionStrings.ConnectionStrings["sphinx_cat"];
                if (connsets != null)
                {
                    connectionString = connsets.ConnectionString;
                }else{
                    connectionString = "character set=utf8;server=127.0.0.1;uid=root;pwd=;database=sphinx_cat";
                }

            }
            return connectionString;
        }

        static public string GetSysBaseData( int  id ){
            string sRet ="";
            MySqlConnection connection = new MySqlConnection(BaseData.GetConnectionString());
            try
            {
                string sql = "SELECT id,value FROM cat_sys_basedata WHERE ID=" + id.ToString();
                MySqlCommand command = new MySqlCommand(sql, connection);
                connection.Open();
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        sRet = reader.GetString("value");
                    }
                }
            }catch(SystemException){}
            return sRet;
        }

        static public string GetUserData(string user_id, string field)
        {
            string sTable = "tmscreators";
            string sRet = "";
            MySqlConnection connection = new MySqlConnection(BaseData.GetConnectionString());
            try
            {
                string sql = "SELECT " + field + " FROM " + sTable + " WHERE user_id='" + user_id + "'";
                MySqlCommand command = new MySqlCommand(sql, connection);
                connection.Open();
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        sRet = reader.GetValue( reader.GetOrdinal( field )).ToString();
                    }
                }
            }
            catch (SystemException) {
                throw new Exception("服务器正在维护，请稍后再访问");
            }
            return sRet;
        }

        static public Boolean SetUserData(int user_id, string field, string value)
        {
            string sTable = "tmscreators";
            int iRet = 0;
            MySqlConnection connection = new MySqlConnection(BaseData.GetConnectionString());
            try
            {
                string sql = "UPDATE " + sTable + " SET " + field + "='"+ value+"' WHERE ID=" + user_id.ToString() ;
                MySqlCommand command = new MySqlCommand(sql, connection);
                connection.Open();
                iRet = command.ExecuteNonQuery();
            }
            catch (SystemException)
            {
                throw new Exception("服务器正在维护，请稍后再访问");
            }
            return iRet >0 ;
        }

        static public string GetUserData(string field, string sTable, string sWhereField, string sWhereValue)
        {
            string sRet = "";
            MySqlConnection connection = new MySqlConnection(BaseData.GetConnectionString());
            try
            {
                string sql = "SELECT " + field + " FROM " + sTable + " WHERE "+ sWhereField +"='" + sWhereValue + "'";
                MySqlCommand command = new MySqlCommand(sql, connection);
                connection.Open();
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        sRet = reader.GetValue(reader.GetOrdinal(field)).ToString();
                    }
                }
            }
            catch (SystemException) { }
            return sRet;
        }
    }


}
